package tech.lq0.mineapex.item;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.UseAction;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.World;
import tech.lq0.mineapex.group.ApexGroup;
import tech.lq0.mineapex.armor.EvoShield;
import tech.lq0.mineapex.registry.ItemRegistry;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

public class ShieldBattery extends Item {
    @Override
    @Nonnull
    @ParametersAreNonnullByDefault
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        ItemStack armor = playerIn.getItemStackFromSlot(EquipmentSlotType.CHEST);
        if (!armor.isEmpty() && armor.getItem().equals(ItemRegistry.EVO_SHIED.get())) {
            if (armor.getStack().getChildTag("evo") == null ||
                    armor.getOrCreateChildTag("evo").getFloat("current") >= armor.getOrCreateChildTag("evo").getFloat("max")) {
                playerIn.sendStatusMessage(new StringTextComponent("当前护盾已满！"), true);
                return ActionResult.resultPass(playerIn.getHeldItem(handIn));
            } else {
                playerIn.setActiveHand(handIn);
            }
        } else {
            playerIn.sendStatusMessage(new StringTextComponent("未装备进化护盾！"), true);
            return ActionResult.resultPass(playerIn.getHeldItem(handIn));
        }
        return super.onItemRightClick(worldIn, playerIn, handIn);
    }

    @Override
    @Nonnull
    @ParametersAreNonnullByDefault
    public ItemStack onItemUseFinish(ItemStack stack, World worldIn, LivingEntity entityLiving) {
        if (entityLiving instanceof PlayerEntity) {
            PlayerEntity player = (PlayerEntity) entityLiving;

            ItemStack armor = player.getItemStackFromSlot(EquipmentSlotType.CHEST);
            if (!armor.isEmpty() && armor.getItem().equals(ItemRegistry.EVO_SHIED.get())) {
                if (armor.getStack().getChildTag("evo") == null) {
                    CompoundNBT nbt = armor.getStack().getOrCreateChildTag("evo");
                    nbt.putFloat("max", EvoShield.shield[0]);
                    nbt.putFloat("current", EvoShield.shield[0]);
                    nbt.putFloat("damage", 0);
                }
                CompoundNBT nbt = armor.getStack().getOrCreateChildTag("evo");
                nbt.putFloat("current", nbt.getFloat("max"));
            }
            if (!player.isCreative()) {
                stack.shrink(1);
            }
            // 充能完成音频
            player.getEntityWorld().playSound(player.getPosX(), player.getPosY(), player.getPosZ(), SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.PLAYERS, 1.0F, 1.0F, true);
        }
        return stack;
    }

    @Override
    @Nonnull
    @ParametersAreNonnullByDefault
    public UseAction getUseAction(ItemStack stack) {
        return UseAction.DRINK;
    }

    @Override
    @ParametersAreNonnullByDefault
    public int getUseDuration(ItemStack stack) {
        return 100;
    }

    public ShieldBattery() {
        super(new Properties().group(ApexGroup.group));
    }
}
